package cn.wikiflyer.beikang.act.user.setting;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;

/**
 * @author maizi UserSettingModifyNameAct.java 修改昵称
 */
public class UserSettingModifyNameAct extends BaseActivity {

	@ViewInject(id = R.id.et_name)
	private EditText et_name;
	@ViewInject(id = R.id.modify_commit)
	private Button btn_commit;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_modify_name);
	}

	@Override
	public void init() {
		btn_commit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (checkEmpty()) {
					new ModifyNameTask(ctx);
				}
			}
		});
	}

	@Override
	public void setData() {
	}

	/**
	 * 检查是否为空， 为空返回false
	 */
	private boolean checkEmpty() {
		if (TextUtils.isEmpty(et_name.getText().toString())) {
			Toast.makeText(ctx, "昵称不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	/**
	 * @author maizi 修改昵称的线程
	 */
	class ModifyNameTask extends WKAsyncTaskPro {
		public ModifyNameTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			String name = et_name.getText().toString();
			return app().net.modifyName(name);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				Toast.makeText(ctx, "修改成功", Toast.LENGTH_SHORT).show();
				finish();
			} else {
				Toast.makeText(ctx, base.info, Toast.LENGTH_SHORT).show();
			}
		}
	}

}
